﻿using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using System;

namespace AgencyManagerSystem.Core
{
    public class RetailUpdaterCreator : BaseUpdaterCreator<RetailModel>
    {
        public RetailUpdater CreateRetailUpdaterInstance()
        {
            RetailUpdater retailUpdater = new RetailUpdater(new RetailFetcher());
            retailUpdater.DataUpdating += FlagAndApproveFlagValid;
            retailUpdater.DataUpdating += NotSpaceCheck;
            retailUpdater.DataUpdating += CodeUniqueCheck;
            retailUpdater.DataUpdating += ReferExist;
            retailUpdater.DataDeleting += MemberExistCheck;
            retailUpdater.DataDeleting += CustomerExistCheck;
            retailUpdater.DataDeleting += OrderExists;
            return retailUpdater;
        }

        private void OrderExists(BaseUpdater<RetailModel> sender, DataDeletingEventArgs<RetailModel> e)
        {
            if (e.Delete == null) return;
            var orderFetcher = new OrderFetcher();
            var orders = orderFetcher.ListByRetail(e.Delete.AutoId);
            if(orders.Count>0)
            {
                e.Message = String.Format("代理商{0}已经产生了订单{1}等{2}笔记录", e.Delete.AutoId, orders[0].AutoId, orders.Count);
                e.Cancel = true;
                return;
            }
        }

        private void CustomerExistCheck(BaseUpdater<RetailModel> sender, DataDeletingEventArgs<RetailModel> e)
        {
            if (e.Delete == null)
                return;
            CustomerFetcher fetcher = new CustomerFetcher();
            var customer = fetcher.ListByRetail(e.Delete.AutoId,"%");
            if (customer.Count > 0)
            {
                e.Message = String.Format("代理商{0}已经产生了客户{1}等{2}笔记录", e.Delete.AutoId, customer[0].AutoId, customer.Count);
                e.Cancel = true;
                return;
            }
        }

        private void ReferExist(BaseUpdater<RetailModel> sender, DataUpdatingEventArgs<RetailModel> e)
        {
            if (e.Insert == null)
                return;
            UserFetcher userFetcher = new UserFetcher();
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            var usr = userFetcher.Get(e.Insert.Sales);
            if (usr==null)
            {
                e.Message = String.Format("对应的业务员{0}不存在", e.Insert.Sales);
                e.Cancel = true;
                return;
            }
            var department = departmentFetcher.Get(e.Insert.Department);
            if (department==null)
            {
                e.Message = String.Format( "对应的部门{0}不存在", e.Insert.Department);
                e.Cancel = true;
                return;
            }
        }

        private void MemberExistCheck(BaseUpdater<RetailModel> sender, DataDeletingEventArgs<RetailModel> e)
        {
            if (e.Delete == null)
                return;
            RetailMemberFetcher fetcher = new RetailMemberFetcher();
            var member = fetcher.ListByRetail(e.Delete.AutoId);
            if (member.Count > 0)
            {
                e.Message = String.Format("[{0}]已存在成员{1}等{2}笔记录", e.Delete.Code,member[0].AutoId,member.Count);
                e.Cancel = true;
                return;
            }
        }

        private void CodeUniqueCheck(BaseUpdater<RetailModel> sender, DataUpdatingEventArgs<RetailModel> e)
        {
            if (e.Insert == null)
                return;
            RetailFetcher retailFetcher = new RetailFetcher();
            var suppiler = retailFetcher.GetByCode(e.Insert.Code);
            if (suppiler == null)
                return;
            if (suppiler.Code == e.Insert.Code && suppiler.AutoId != e.Insert.AutoId)
            {
                e.Message = String.Format("代号[{0}]不唯一", e.Insert.Code);
                e.Cancel = true;
                return;
            }
        }

        private void NotSpaceCheck(BaseUpdater<RetailModel> sender, DataUpdatingEventArgs<RetailModel> e)
        {
            if (e.Insert == null)
                return;
            if (String.IsNullOrWhiteSpace(e.Insert.Code) || String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "代号或者名称不能空白";
                e.Cancel = true;
                return;
            }
        }
    }
}
